package com.wao.cms.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.wao.cms.service.FooterService;
import com.wao.cms.service.dto.FooterDTO;
import com.wao.cms.web.rest.util.HeaderUtil;
import com.wao.cms.web.rest.util.PaginationUtil;
import io.github.jhipster.web.util.ResponseUtil;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing Footer.
 */
@RestController
@RequestMapping("/api")
public class FooterResource {

    private final Logger log = LoggerFactory.getLogger(FooterResource.class);

    private static final String ENTITY_NAME = "footer";

    private final FooterService footerService;

    public FooterResource(FooterService footerService) {
        this.footerService = footerService;
    }

    /**
     * POST  /footers : Create a new footer.
     *
     * @param footerDTO the footerDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new footerDTO, or with status 400 (Bad Request) if the footer has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/footers")
    @Timed
    public ResponseEntity<FooterDTO> createFooter(@Valid @RequestBody FooterDTO footerDTO) throws URISyntaxException {
        log.debug("REST request to save Footer : {}", footerDTO);
        if (footerDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new footer cannot already have an ID")).body(null);
        }
        FooterDTO result = footerService.save(footerDTO);
        return ResponseEntity.created(new URI("/api/footers/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /footers : Updates an existing footer.
     *
     * @param footerDTO the footerDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated footerDTO,
     * or with status 400 (Bad Request) if the footerDTO is not valid,
     * or with status 500 (Internal Server Error) if the footerDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/footers")
    @Timed
    public ResponseEntity<FooterDTO> updateFooter(@Valid @RequestBody FooterDTO footerDTO) throws URISyntaxException {
        log.debug("REST request to update Footer : {}", footerDTO);
        if (footerDTO.getId() == null) {
            return createFooter(footerDTO);
        }
        FooterDTO result = footerService.save(footerDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, footerDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /footers : get all the footers.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of footers in body
     */
    @GetMapping("/footers")
    @Timed
    public ResponseEntity<List<FooterDTO>> getAllFooters(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of Footers");
        Page<FooterDTO> page = footerService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/footers");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /footers/:id : get the "id" footer.
     *
     * @param id the id of the footerDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the footerDTO, or with status 404 (Not Found)
     */
    @GetMapping("/footers/{id}")
    @Timed
    public ResponseEntity<FooterDTO> getFooter(@PathVariable Long id) {
        log.debug("REST request to get Footer : {}", id);
        FooterDTO footerDTO = footerService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(footerDTO));
    }

    /**
     * DELETE  /footers/:id : delete the "id" footer.
     *
     * @param id the id of the footerDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/footers/{id}")
    @Timed
    public ResponseEntity<Void> deleteFooter(@PathVariable Long id) {
        log.debug("REST request to delete Footer : {}", id);
        footerService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    /**
     * GET  /websites/{id}/footers : get the footer of the website.
     *
     * @param id the id of the Website
     * @return the ResponseEntity with status 200 (OK) and the list of headers in body
     */
    @GetMapping("/websites/{id}/footers")
    @Timed
    public ResponseEntity<FooterDTO> getFooterByWebsite(@PathVariable Long id) {
        log.debug("REST request to get a page of Headers");
        Page<FooterDTO> page = footerService.findByWebsiteId(id, new PageRequest(0, 1));
        if (page == null || !page.hasContent() || page.getContent().get(0) == null)
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        return ResponseEntity.ok().body(page.getContent().get(0));
    }
}
